from asyncore import poll


openFile = open( '/Users/k.ji/vscode/PycharmProjects/AlteonPY/chinamoney/hxadA20221101' )
transFile = open('/Users/k.ji/vscode/PycharmProjects/AlteonPY/chinamoney/hxadA20221101-after','a')

initConfig = openFile.read()
readConfig = initConfig.split('\n')
configLenth = len(readConfig)




poolNameList = []
for i in range(configLenth):
    if 'appdirector farm server table create' in readConfig[i]:
        poolName = readConfig[i].split(' ')[5]
        poolNameList.append(poolName)
poolNameList = list(set(poolNameList))


for poolName in poolNameList:
    poolConfig = 'tmsh create ltm pool ' + poolName + ' members add { '
    transFile.write(poolConfig)
    for i in range(configLenth):
        if 'appdirector farm server table create' in readConfig[i] and poolName == readConfig[i].split(' ')[5]:
            memberIP = readConfig[i].split(' ')[6]
            memberPort = readConfig[i].split(' ')[7]
            memberAndPort = memberIP + ":" + memberPort
            memberAddConfig = memberAndPort + " "
            transFile.write(memberAddConfig)
        elif 'appdirector farm server table create' in readConfig[i] and 'appdirector farm server table create' not in readConfig[i+1]:
            poolConfigEnd = " } monitor tcp load-balancing-mode round-robin"
            transFile.write(poolConfigEnd + '\n')
            break
        
            


for i in range(configLenth):
   if 'appdirector l4-policy table create' in readConfig[i]:      #修改配置的入口判断
        vip = readConfig[i].split(' ')[4]
        poolName = readConfig[i].split(' ')[10]
        vsName = readConfig[i].split(' ')[8]
        vsPort = readConfig[i].split(' ')[6]
        f5Config = 'tmsh create ltm virtual %s destination %s:%s pool %s ip-protocol tcp source-address-translation { type automap } persist replace-all-with { source_addr }' %(vsName,vip,vsPort,poolName)
        transFile.write(f5Config + '\n')

       
       


        



  


